<?php
namespace app\shop\controller;
use think\Db;
use app\admin\model\ShopStore as ShopStoreModel;

class ShopStore extends Common
{
    //门店列表
    public function lst()
    {
        $shopId = session('shopsh_id');
        $where['a.shop_id'] = $shopId;
        $where['a.deleted'] = 0;
        $list = Db::name('shop_store')->alias('a')
            ->field('a.id,a.name,a.logo,a.phone,a.contact,a.collate,a.addtime,a.status')
            ->where($where)
            ->order('a.id asc')
            ->paginate(10);

        $page = $list->render();

        if(input('page')){
            $pnum = input('page');
        }else{
            $pnum = 1;
        }
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->assign('pnum', $pnum);
        if (request()->isAjax()) {
            return $this->fetch('ajaxpage');
        } else {
            return $this->fetch();
        }
    }


    public function teng_xun_map(){
        return $this->fetch();
    }


    public function checkAdname()
    {
        if (request()->isAjax()) {
            $id = input('post.id');
            if(isset($id) && !empty($id)){
                $ad_name = Db::name('shop_store')->where('id','<>',$id)->where('name', input('post.name'))->where('deleted',0)->find();
            }else{
                $ad_name = Db::name('shop_store')->where('name', input('post.name'))->where('deleted',0)->find();
            }
            if ($ad_name) {
                echo 'false';
            } else {
                echo 'true';
            }
        }
    }

    //根据门店位置获取门店列表
    public function poslist()
    {
        $id = input('cate_id');
        $cate_name = Db::name('ad_cate')->where('id', $id)->value('cate_name');
        $adCateList = Db::name('ad_cate')->field('id,cate_name,width,height')->order('id asc')->select();
        $list = Db::name('ad')->alias('a')->field('a.id,a.ad_name,a.is_on,b.cate_name')->join('sp_ad_cate b', 'a.cate_id = b.id', 'LEFT')->where('a.cate_id', $id)->order('a.id desc')->paginate(25);
        $page = $list->render();
        if (input('page')) {
            $pnum = input('page');
        } else {
            $pnum = 1;
        }

        $this->assign('cate_id', $id);
        $this->assign('cate_name', $cate_name);
        $this->assign('pnum', $pnum);
        $this->assign('adCateList', $adCateList);
        $this->assign('list', $list);
        $this->assign('page', $page);
        if (request()->isAjax()) {
            return $this->fetch('ajaxpage');
        } else {
            return $this->fetch('lst');
        }
    }

    //修改门店状态
    public function gaibian()
    {
        $id = input('post.id');
        $name = input('post.name');
        $value = input('post.value');
        $data[$name] = $value;
        $data['id'] = $id;
        $ads = Db::name('shop_store')->where('id', $data['id'])->find();
        if ($ads) {
            $count = Db::name('shop_store')->update($data);
            if ($count > 0) {
                if ($value == 1) {
                    // Db::name('ad')->where('cate_id',$ads['cate_id'])->where('id','neq',$ads['id'])->update(array('is_on'=>0));
                    ys_admin_logs('启用门店', 'shop_store', $id);
                } elseif ($value == 0) {
                    ys_admin_logs('禁用门店', 'shop_store', $id);
                }
                $result = 1;
            } else {
                $result = 0;
            }
        } else {
            $result = 0;
        }
        return $result;
    }

    public function deleteone()
    {
        if (input('post.ypic_id') && input('post.ad_id')) {
            $pics = Db::name('ad_pic')->where('id', input('post.ypic_id'))->where('ad_id', input('post.ad_id'))->field('id,pic')->find();
            if ($pics) {
                $count = Db::name('ad_pic')->delete(input('post.ypic_id'));
                if ($count > 0) {
                    if (!empty($pics['pic']) && file_exists('./' . $pics['pic'])) {
                        @unlink('./' . $pics['pic']);
                    }
                    $value = 1;
                } else {
                    $value = 0;
                }
            } else {
                $value = 0;
            }
        } else {
            $value = 0;
        }
        return json($value);
    }

    //添加门店
    public function add()
    {
        if (request()->isAjax()) {
            $shopId = session('shopsh_id');
            $data = input('post.');
            $data['shop_id'] = $shopId;
            $result = $this->validate($data,'ShopStore');
            if(true !== $result){
                $value = array('status'=>0,'mess'=>$result);
            }else{
                $pros = Db::name('province')->where('id',$data['pro_id'])->where('checked',1)->where('pro_zs',1)->field('id,pro_name')->find();
                $data['province'] = $pros['pro_name'];
                $citys = Db::name('city')->where('id',$data['city_id'])->where('pro_id',$pros['id'])->where('checked',1)->where('city_zs',1)->field('id,city_name')->find();
                $data['city'] = $citys['city_name'];
                $areas = Db::name('area')->where('id',$data['area_id'])->where('city_id',$citys['id'])->where('checked',1)->field('id,area_name')->find();
                $data['area'] = $areas['area_name'];
                $latlon = explode(',',$data['latlon']);
                $data['latitude'] = $latlon[0];
                $data['longitude'] = $latlon[1];
                $data['addtime'] = time();
                $data['updatetime'] = time();
                $shopStoreModel = new ShopStoreModel();
                $shopStoreModel->data($data);
                $lastId = $shopStoreModel->allowField(true)->save();
                if($lastId){
//                    ys_admin_logs('新增门店','shop_store',$lastId->id);
                    $value = array('status'=>1, 'mess'=>'增加成功');
                }else{
                    $value = array('status'=>0, 'mess'=>'增加失败');
                }
            }
            return json($value);

//            if($add){
//                datamsg(1,'新增成功');
//                ys_admin_logs('新增门店','ad',$adModel->id);
//            }else{
//                datamsg(0,'新增失败');
//            }
        } else {
            $prores = Db::name('province')->where('checked',1)->where('pro_zs',1)->field('id,pro_name,zm')->order('sort asc')->select();
            $cityres = Db::name('city')->where('pro_id',$prores[0]['id'])->field('id,city_name,zm')->order('sort asc')->select();
            $areares = Db::name('area')->where('city_id',$cityres[0]['id'])->field('id,area_name,zm')->select();
            $this->assign('prores',$prores);
            $this->assign('cityres',$cityres);
            $this->assign('areares',$areares);
            return $this->fetch();
        }
    }

    //编辑门店
    public function edit()
    {
        if (request()->isAjax()) {
            if (input('post.id')) {
                $data = input('post.');

                $result = $this->validate($data, 'ShopStore');
                if (true !== $result) {
                    datamsg(0,$result);
                }
                $pros = Db::name('province')->where('id',$data['pro_id'])->where('checked',1)->where('pro_zs',1)->field('id,pro_name')->find();
                $data['province'] = $pros['pro_name'];
                $citys = Db::name('city')->where('id',$data['city_id'])->where('pro_id',$pros['id'])->where('checked',1)->where('city_zs',1)->field('id,city_name')->find();
                $data['city'] = $citys['city_name'];
                $areas = Db::name('area')->where('id',$data['area_id'])->where('city_id',$citys['id'])->where('checked',1)->field('id,area_name')->find();
                $data['area'] = $areas['area_name'];

                $latlon = explode(',',$data['latlon']);
                $data['latitude'] = $latlon[0];
                $data['longitude'] = $latlon[1];
                unset($data['latlon']);
                $shopStoreModel = new ShopStoreModel();
                $edit = $shopStoreModel->where('id',$data['id'])->update($data);
                if($edit !== false){
                    datamsg(1, '编辑成功');
                }else{
                    datamsg(0, '编辑失败');
                }
            } else {
                datamsg(0, '缺少参数');
            }

        } else {
            if (input('id')) {
                $id = input('id');
                $info = Db::name('shop_store')->find($id);
                if ($info) {
                    $prores = Db::name('province')->where('checked',1)->where('pro_zs',1)->field('id,pro_name,zm')->order('sort asc')->select();
                    $cityres = Db::name('city')->where('pro_id',$info['pro_id'])->field('id,city_name,zm')->order('sort asc')->select();
                    $areares = Db::name('area')->where('city_id',$info['city_id'])->field('id,area_name,zm')->select();
                    $this->assign('prores',$prores);
                    $this->assign('cityres',$cityres);
                    $this->assign('areares',$areares);

                    $this->assign('info',$info);
                    return $this->fetch();
                } else {
                    $this->error('找不到相关信息');
                }
            } else {
                $this->error('缺少参数');
            }
        }
    }


    //删除门店
    public function delete()
    {
        if (input('post.id')) {
            $id = array_filter(explode(',', input('post.id')));
        } else {
            $id = input('id');
        }
        //查看该门店下面是否有员工
        $shopStorestaffCount = Db::name("shop_storestaff")->where("deleted",0)->where("ss_id",'in',$id)->count();
        if($shopStorestaffCount){
            return json(array('status' => 0, 'mess' => '该门店下面有员工，请先删除员工'));
        }

        if (!empty($id)) {
            if (is_array($id)) {
                $delId = implode(',', $id);
                $arr2 = Db::name('shop_store')->where('id', 'in', $delId)->update(['deleted'=>1]);
            } else {
                $arr2 = Db::name('shop_store')->where('id', $id)->update(['deleted'=>1]);
            }
            if ($arr2) {
                $value = array('status' => 1, 'mess' => '删除成功');
            } else {
                $value = array('status' => 0, 'mess' => '删除失败');
            }
        } else {
            $value = array('status' => 0, 'mess' => '请选择删除项');
        }
        return json($value);
    }


    //搜索门店
    public function search()
    {
        $shopId = session('shopsh_id');
        if (input('post.keyword')) {
            cookie('name', input('post.keyword'), 3600);
        }

        if (cookie('ad_name')) {
            $where['a.name'] = array('like', '%' . cookie('name') . '%');
        }
        $where['a.shop_id'] = ["=",$shopId];

        $list = Db::name('shop_store')
            ->alias('a')
            ->field('a.*')
            ->where($where)
            ->order('a.id desc')
            ->paginate(25);
        $page = $list->render();
        if (input('page')) {
            $pnum = input('page');
        } else {
            $pnum = 1;
        }
        if (cookie('name')) {
            $this->assign('name', cookie('name'));
        }
        $this->assign('pnum', $pnum);
        $this->assign('list', $list);// 赋值数据集
        $this->assign('page', $page);// 赋值分页输出
        if (request()->isAjax()) {
            return $this->fetch('ajaxpage');
        } else {
            return $this->fetch('lst');
        }
    }
}